/********************************difference-in-differences analysis of US-JP Twins********************************
Main purpose: compare forward citations and citation lags for US-JP twins around AIPA
datasets:
us_ep_twin: US-EP twins, constructed based on data from PATSTAT 2017
us_jp_twin: US-JP twins that do not have EP equivalents, constructed based on data from PATSTAT 2017
********************************difference-in-differences analysis of US-JP Twins********************************/






******************Figure D2: normalized forward citations US-JP twins******************
global chg=tm(2000m12) 
use "$DATA\us_jp_twin",clear
preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
gen fcite_pub=fcite_pub3 //select graph variable
winsor2 fcite_pub, by(uspto)
bys month uspto: egen mean=mean(fcite_pub_w)
keep month uspto mean 
bys month uspto: keep if _n==1
*normalize
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0, msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Normalized 3-yr forward cites", size(medsmall)) xtitle("") ///
legend(off) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) ylab(,nogrid)
graph export "$DATA\usjp_fcite_pub3notri.eps", replace
restore

preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
gen fcite_pub=fcite_pub5 //select graph variable
winsor2 fcite_pub, by(uspto)
bys month uspto: egen mean=mean(fcite_pub_w)
keep month uspto mean 
bys month uspto: keep if _n==1
*normalize
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0, msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Normalized 5-yr forward cites", size(medsmall)) xtitle("") ///
legend(off) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) ylab(,nogrid)
graph export "$DATA\usjp_fcite_pub5notri.eps", replace
restore


preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
gen fcite_pub=fcite_pub7 //select graph variable
winsor2 fcite_pub, by(uspto)
bys month uspto: egen mean=mean(fcite_pub_w)
keep month uspto mean 
bys month uspto: keep if _n==1
*normalize
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0, msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Normalized 10-yr forward cites", size(medsmall)) xtitle("") ///
legend(region(color(white)) order(1 "US" 2 "JP")) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) ylab(,nogrid)
graph export "$DATA\usjp_fcite_pub7notri.eps", replace
restore

preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
gen fcite_pub=fcite_pub10 //select graph variable
winsor2 fcite_pub, by(uspto)
bys month uspto: egen mean=mean(fcite_pub_w)
keep month uspto mean 
bys month uspto: keep if _n==1
*normalize
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0, msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Normalized 10-yr forward cites", size(medsmall)) xtitle("") ///
legend(region(color(white)) order(1 "US" 2 "JP")) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) ylab(,nogrid)
graph export "$DATA\usjp_fcite_pub10notri.eps", replace
restore




******************Table 7: US-EP versus US-JP twin analyses******************
use "$DATA\us_jp_twin",clear
gen us_ep=0
append using "$DATA\us_ep_twin" //stack US-EP twins and US-JP twins

gen post_uspto=post_aipa*uspto
gen grant18m_uspto=grant18m*uspto
gen month_us=month if uspto==1 
gen month_ep=month if uspto==0 & us_ep==1
gen month_jp=month if uspto==0 & us_ep==0
replace month_us=0 if month_us==.
replace month_ep=0 if month_ep==.
replace month_jp=0 if month_jp==.



local dv_list "lnfcite_pub3 lnfcite_pub5 lnfcite_pub7 lnfcite_pub10 lnlag1 lnlag3 lnlag5 lnlag7" 
foreach dv of local dv_list{
	reghdfe `dv' uspto post_uspto granted grant18m grant18m_uspto month_jp if us_ep==0, a(appln_id_us month) vce(cluster month_country) //US-JP twins
	outreg2 using "$DATA\table7.xls", bdec(3) ctitle(`dv') nocons adjr2
	reghdfe `dv' uspto post_uspto granted grant18m grant18m_uspto month_ep if us_ep==1, a(appln_id_us month) vce(cluster month_country) //US-EP twins
	outreg2 using "$DATA\table7.xls", bdec(3) ctitle(`dv') nocons adjr2
}
rm "$DATA\table7.txt"

//compare the coef b/w US-EP and US-JP twins
log using "$DATA\table7_log.txt", replace
preserve
local dv_list "lnfcite_pub3 lnfcite_pub5 lnfcite_pub7 lnfcite_pub10 lnlag1 lnlag3 lnlag5 lnlag7" 
foreach dv of local dv_list{
hdfe `dv' uspto post_uspto granted grant18m grant18m_uspto month_ep if us_ep==1, a(appln_id_us month) gen(R1_)
hdfe `dv' uspto post_uspto granted grant18m grant18m_uspto month_jp if us_ep==0, a(appln_id_us month) gen(R0_)
qui: reg  R1_`dv' R1_uspto-R1_month_ep, nocons
est store r1
qui: reg  R0_`dv' R0_uspto-R0_month_jp, nocons
est store r0
suest r0 r1, vce(cluster month_country)
lincom [r0_mean]R0_post_uspto- [r1_mean]R1_post_uspto //compare the coef across two sub-samples
drop R1_* R0_*
}
restore
log close
